require.config({
    baseUrl: 'js/common'
});
require(['common', 'pagin'], function(defaultStr, pagin) {
    var baseUrl = defaultStr();
    /*
     *------助管信息管理
     *增加助管：addZhuguan
     *删除助管：deleteZG
     *查询数据：queryData
     *向服务器获取所有数据：getAllData
     *向服务器查询数据（页）：getPage
     *获取学院信息列表，获取银行列表：getDept,getBanks，目前是写死的
     *说明：默认每页显示5条记录
     *初始化，向服务器获取前5条记录，以及总的记录条数，再更新分页
     *-------
     */
    var zhuguan_info = new Vue({
        el: '#zhuguan-data',
        data: {
            /*查询对象*/
            query: {
                "rows": 3,
                "page": 1,
                // "bankNo": '',
                // "deptId": ''
            },
            stu_query: {
                // "stuId": "111111",
                // "stuName": "fsasd",
                // "deptId": "006",
                // "dsId": "12312",
                // "dsName": "test",
                "rows": 5,
                "page": 1
            },
            /*每页显示的列数*/
            rows: 3,
            /*pageNum 总页数*/
            pageNum: 1,
            /*当前页码*/
            curPage: 1,
            /*pages用于构造page*/
            pages: [],
            /*每页显示的列数*/
            stu_rows: 5,
            /*pageNum 总页数*/
            stu_pageNum: 1,
            /*当前页码*/
            stu_curPage: 1,
            /*pages用于构造page*/
            stu_pages: [],
            /*用于提取编辑学生*/
            stu_edit: '',
            /*部门列表*/
            depts: [],
            /*银行列表*/
            banks: [{
                "bankNo": "1",
                "bankName": "中国银行"
            }, {
                "bankNo": "2",
                "bankName": "建设银行"
            }],
            /*列表中的助管项目*/
            items: [],
            /*所有学生*/
            stu_items: [],
            /*选中删除的助管对象*/
            deleteItem: '',
            /*要添加的助管对象*/
            addItem: '',
            currentView: '',
            total: 0,
            check_stuId: [],
            url: {
                "addZhuguan": baseUrl + "/base/stu/insertStuToAst.do",
                "queryZhuguan": baseUrl + "/base/stu/list.do",
                "queryStu": baseUrl + "/base/stu/searchStudent.do",
                "delZhuguan": baseUrl + "/base/stu/delete.do",
                "editZhuguan":baseUrl+"/base/stu/update.do",
                'getDepts': baseUrl + '/base/dept/list.do',
                "uploadFile": baseUrl + '/base/stu/upload.do',
                "downloadMB": baseUrl + '/base/stu/downloadMB.do'
            }
        },
        methods: {
            /*初始化*/
            init: function() {
                var _self = this;
                // this.getBanks();
                _self.queryData();
                _self.getDepts();
                _self.queryStuData();
            },
            /*查询数据*/
            queryData: function(str) {
                var _self = this;
                if (str) {
                    _self.query.page=1;
                    _self.curPage=1;
                }
                var query = JSON.stringify(_self.query);
                console.log(query);
                $.ajax({
                    type: "POST",
                    url: _self.url.queryZhuguan,
                    data: query,
                    contentType: "application/json; charset=utf-8",
                    dataType: 'json',
                    success: function(result) {
                        console.log(result);
                        _self.total = result.total;
                        _self.pageNum = Math.ceil(result.total / _self.rows);
                        _self.items = [];
                        $.each(result.result, function(i, value) {
                            _self.items.push(value);
                        });
                        /*更新分页*/
                        var result = pagin.paginGetli(_self.pageNum, _self.curPage);
                        _self.pages = result.pages;
                    }
                });

            },
            /*查询学生数据*/
            queryStuData: function(str) {
                var _self = this;
                if (str) {
                    _self.stu_query.page=1;
                    _self.stu_curPage=1;
                }
                var stu_query = JSON.stringify(_self.stu_query);
                console.log(stu_query);
                $.ajax({
                    type: "POST",
                    url: _self.url.queryStu,
                    data: stu_query,
                    contentType: "application/json; charset=utf-8",
                    dataType: 'json',
                    success: function(result) {
                        console.log(result);
                        _self.stu_total = result.total;
                        _self.stu_pageNum = Math.ceil(result.total / _self.stu_rows);
                        _self.stu_items = [];
                        $.each(result.result, function(i, value) {
                            _self.stu_items.push(value);
                        });
                        /*更新分页*/
                        console.log(_self.stu_pageNum);
                        var result = pagin.paginGetli(_self.stu_pageNum, _self.stu_curPage);
                        _self.stu_pages = result.pages;
                    }
                });
            },
            /*根据row和page请求数据*/
            getDataPage: function(cur_page) {
                var _self = this;
                _self.query.page = cur_page;
                _self.curPage = cur_page;
                _self.queryData();
            },
            /*处理助管列表分页查询*/
            submitPage: function(p) {
                var _self = this;
                console.log(p);
                if (p === ">>") {
                    _self.curPage++;
                    _self.getDataPage(_self.curPage);
                } else if (p === "<<") {
                    _self.curPage--;
                    _self.getDataPage(_self.curPage);
                } else {
                    _self.curPage = p;
                    _self.getDataPage(_self.curPage);
                }
            },
            /*根据row和page请求数据*/
            getStuPage: function(p) {
                var _self = this;
                console.log(p);
                _self.stu_query.page = p;
                _self.stu_curPage = p;
                _self.queryStuData();
            },
            /*处理学生列表分页查询*/
            submitStuPage: function(p) {
                var _self = this;
                console.log(p);
                if (p === ">>") {
                    _self.stu_curPage++;
                    _self.getStuPage(_self.stu_curPage);
                } else if (p === "<<") {
                    _self.stu_curPage--;
                    _self.getStuPage(_self.stu_curPage);
                } else if (p === "...") {
                    _self.getStuPage(_self.stu_curPage + 5);

                } else {
                    _self.stu_curPage = p;
                    _self.getStuPage(_self.stu_curPage);
                }
            },
            /*获取部门名称以及列表*/
            getDepts: function() {
                var _self = this;
                var query = {
                    "rows": 10,
                    "page": 1
                }
                query = JSON.stringify(query);
                console.log(query);
                $.ajax({
                    type: "POST",
                    url: _self.url.getDepts,
                    data: query,
                    contentType: "application/json; charset=utf-8",
                    dataType: 'json',
                    success: function(result) {
                        console.log(result);
                        _self.depts = [];
                        $.each(result.result, function(i, value) {
                            _self.depts.push(value);
                        })
                    }
                });
                /*更新分页*/
                _self.pages = pagin.paginGetli(_self.page, _self.curPage).pages;

            },
            /*获取银行列表*/
            getBanks: function() {
                var _self = this;
                $.ajax({
                    type: "GET",
                    url: '../json/allDates.json',
                    dataType: "json",
                    success: function(result) {
                        $.each(result, function(i, value) {
                            _self.banks.push(value);
                        })
                    }
                });
            },

            /*添加学生为助管*/
            addZhuguan: function() {
                var _self = this;
                var id = '';
                var len = _self.check_stuId.length;
                for (i in _self.check_stuId) {
                    if (i == len - 1) {
                        id = id + _self.check_stuId[i];
                    } else {
                        id = id + _self.check_stuId[i] + ',';
                    }
                }
                var data = {
                    "stuIds": id
                };
                $.ajax({
                    type: "POST",
                    url: _self.url.addZhuguan,
                    dataType: "json",
                    contentType: "application/json; charset=utf-8",
                    data: JSON.stringify(data),
                    success: function(result) {
                        alert(result.message);
                        _self.queryData();
                        $('#myModal-add').modal('hide')
                    }
                });
            },
            /*删除一条助管信息*/
            deleteZG: function(i, name) {
                var _self = this;
                _self.deleteItem = {
                    "stuID": i,
                    "name": name
                };
            },
            confirmDel: function() {
                var _self = this;
                var deleteItem = JSON.stringify({
                    "stuID": _self.deleteItem.stuID
                });
                console.log(deleteItem);
                $.ajax({
                    type: "POST",
                    url: _self.url.delZhuguan,
                    data: deleteItem,
                    contentType: "application/json; charset=utf-8",
                    dataType: 'json',
                    success: function(result) {
                        alert(result.message);
                        _self.queryData();
                        $('#myModal-delete').modal('hide')
                    }
                });
            },
            /*编辑一条助管信息*/
            editZG: function(i, name) {
                var _self = this;
                _self.stu_edit= {
                    "stuId": i,
                    "name": name
                };
            },
            confirmEdit: function() {
                var _self = this;
                var editItem = JSON.stringify(_self.stu_edit);
                console.log(editItem);
                $.ajax({
                    type: "POST",
                    url: _self.url.editZhuguan,
                    data: editItem,
                    contentType: "application/json; charset=utf-8",
                    dataType: 'json',
                    success: function(result) {
                        alert(result.message);
                        _self.queryData();
                        $('#myModal-modify').modal('hide')
                    }
                });
            },
            /*上传文件*/
            upload: function() {
                var formData = new FormData();
                formData.append('uploadExcel', $('#file')[0].files[0]);
                $.ajax({
                    url: this.url.uploadFile,
                    type: 'POST',
                    cache: false,
                    data: formData,
                    processData: false,
                    contentType: false
                }).done(function(res) {}).fail(function(res) {});
            }
        }

    });
    zhuguan_info.init();
});
